app.controller('BusShiftModalCtrl', function ($scope, $modalInstance, param, ajax, toaster) {

	$scope.vehiclelist = [];
	$scope.driverlist = [];
	$scope.param = param;
	if ($scope.param.data == null) $scope.param.data = {};
	$scope.tickets = $scope.param.data.tickets;

	//获取可用的司机列表
	$scope.loadDriverList = function () {
		ajax.call(new Req.GET({
			resource: 'driver/inactive'
		})).then(function (result) {
			$scope.driverlist = result.result;
			if ($scope.param.data.driverId > 0) {
				for (var i in $scope.driverlist) {
					if ($scope.param.data.driverId != $scope.driverlist[i].id) continue;
					$scope.driverlist.selected = $scope.driverlist[i];
					break;
				}
			}
		}, function () { });
	};
	$scope.loadDriverList();

	//获取可用的车辆列表
	$scope.loadVehicleList = function () {
		ajax.call(new Req.GET({
			resource: 'vehicle/bs/ok'
		})).then(function (result) {
			$scope.vehiclelist = result.result;
			if ($scope.param.data.vehicleId > 0) {
				for (var i in $scope.vehiclelist) {
					if ($scope.param.data.vehicleId != $scope.vehiclelist[i].id) continue;
					$scope.vehiclelist.selected = $scope.vehiclelist[i];
					break;
				}
			}
		}, function () { });
	};
	$scope.loadVehicleList();

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.tickets != $scope.param.data.tickets && $scope.param.data.tickets <= 0) {
				toaster.pop('warning', '提示', '车票数必须大于0');
				return;
			}

			$scope.param.data.stationFilter = null;
			if ($scope.driverlist.selected != undefined) $scope.param.data.driverId = $scope.driverlist.selected.id;
			if ($scope.vehiclelist.selected != undefined) $scope.param.data.vehicleId = $scope.vehiclelist.selected.id;
			$modalInstance.close($scope.param);
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});